package com.zz.inventory.service.impl;

import java.util.Date;
import java.util.List;

import com.zz.inventory.mapper.LabsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zz.inventory.mapper.ClientsMapper;
import com.zz.inventory.domain.Clients;
import com.zz.inventory.service.IClientsService;

/**
 * 客户端管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-02
 */
@Service
public class ClientsServiceImpl implements IClientsService 
{
    @Autowired
    private ClientsMapper clientsMapper;
    @Autowired
    private LabsMapper labsMapper;

    /**
     * 查询客户端管理
     * 
     * @param clientId 客户端管理主键
     * @return 客户端管理
     */
    @Override
    public Clients selectClientsByClientId(Long clientId)
    {
        return clientsMapper.selectClientsByClientId(clientId);
    }

    /**
     * 查询客户端管理列表
     * 
     * @param clients 客户端管理
     * @return 客户端管理
     */
    @Override
    public List<Clients> selectClientsList(Clients clients)
    {
        return clientsMapper.selectClientsList(clients);
    }

    /**
     * 新增客户端管理
     * 
     * @param clients 客户端管理
     * @return 结果
     */
    @Override
    public int insertClients(Clients clients) {
        // 1. 校验机房ID是否有效
        if (!clientsMapper.checkLabIdExists(clients.getLabId())) {
            throw new IllegalArgumentException("机房ID不存在");
        }

        // 2. 校验MAC地址唯一性
        if (clientsMapper.checkMacAddressExists(clients.getMacAddress())) {
            throw new IllegalArgumentException("MAC地址已存在");
        }

        // 3. 填充默认值
        clients.setStatus("offline"); // 需求：默认离线
        clients.setRegistrationTime(new Date()); // 注册时间默认当前时间

        // 4. 执行插入
        return clientsMapper.insertClients(clients);
    }

    /**
     * 修改客户端管理
     * 
     * @param clients 客户端管理
     * @return 结果
     */
    @Override
    public int updateClients(Clients clients)
    {
        //如果手动设置状态为 online，自动更新心跳时间
        if ("online".equals(clients.getStatus())){
            clients.setLastHeartbeat(new Date());
        }
        return clientsMapper.updateClients(clients);
    }

    /**
     * 批量删除客户端管理
     * 
     * @param clientIds 需要删除的客户端管理主键
     * @return 结果
     */
    @Override
    public int deleteClientsByClientIds(Long[] clientIds)
    {
        return clientsMapper.deleteClientsByClientIds(clientIds);
    }

    /**
     * 删除客户端管理信息
     * 
     * @param clientId 客户端管理主键
     * @return 结果
     */
    @Override
    public int deleteClientsByClientId(Long clientId)
    {
        return clientsMapper.deleteClientsByClientId(clientId);
    }

    @Override
    public List<Clients> selectClientsByHeartbeat(Date threshold) {
        return clientsMapper.selectClientsByHeartbeat(threshold);
    }
    @Override
    public List<Clients> selectClientsByOnline(Date threshold) {
        return clientsMapper.selectClientsByOnline(threshold);
    }

    @Override
    public List<Clients> selectClientsWithNoHeartbeat() {
        return clientsMapper.selectClientsWithNoHeartbeat();
    }
}
